#include <iostream>
#include <thread>
#include <exception>
#include <utility>
using namespace std;

namespace test
{

class exception
{
public:
    exception(int id, const string& msg)
        : _err_id(id), _err_msg(msg)
    {}

    virtual const string what() const
    {
        return _err_msg;
    }

    int get_id() const
    {
        return _err_id;
    }

protected:
    int _err_id;
    string _err_msg;
};

class sql_exception : public exception
{
public:
    sql_exception(int id, const string& msg, const string& sql)
        : exception(id, msg)
        , _sql(sql)
    {}

    virtual const string what() const
    {
        return  "sql_exception " + _sql;
    }

protected:
    string _sql;
};

class cache_exception : public exception
{
public:
    cache_exception(int id, const string& msg)
        : exception(id, msg)
    {}

    virtual const string what() const
    {
        return  "cache_exception " + _err_msg;
    }
};

class net_exception : public exception
{
public:
    net_exception(int id, const string& msg, const string& type)
        : exception(id, msg)
        , _type(type)
    {}

    virtual const string what() const
    {
        return "net_exception " + _err_msg + ": " + _type;
    }

protected:
    string _type;
};

void sql_mgr()
{
    srand(time(0));
    if (rand() % 2 == 0)
    {
        throw sql_exception(100, "insufficient permissions", "select *");
    }
    else if (rand() % 8 == 0)
    {
        throw sql_exception(200, "sql error", "use mysql;");
    }
    else
    {
        cout << "success" << endl;
    }
}

void cache_mgr()
{
    srand(time(0));
    if (rand() % 5 == 0)
    {
        throw cache_exception(100, "insufficient permissions");
    }
    else if (rand() % 6 == 0)
    {
        throw cache_exception(200, "data is not exists");
    }

    sql_mgr();
}

void http_server()
{
    srand(time(0));
    if (rand() % 3 == 0)
    {
        throw net_exception(100, "insufficient permissions", "get");
    }
    else if (rand() % 4 == 0)
    {
        throw net_exception(200, "network abnormal", "post");
    }

    cache_mgr();
}

}

int main()
{
    while (1)
    {
        try
        {
            test::http_server();
        }
        catch (const test::exception& e)
        {
            cout << e.what() << endl;
        }
        catch (...)
        {
            cout << "unkown exception" << endl;
        }

        this_thread::sleep_for(chrono::seconds(1));
    }

    return 0;
}



double division(int a, int b)
{
    if (b == 0)
        throw "division by zero";
    else
        return a * 1.0 / b;
}

void func()
{
    int a = 0, b = 0;
    cin >> a >> b;

    cout << division(a, b) << endl;
}

int main()
{
    try
    {
        try
        {
            func();
        }
        catch(...)
        {}
    }
    catch (const char* s)
    {
        cout << s << endl;
    }
    catch (...)
    {
        cout << "unkown exception" << endl;
    }

    return 0;
}
